package common.kingdee.domain;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.springframework.beans.factory.annotation.Configurable;

import common.security.domain.Department;
@Configurable
public class KingdeeDepartment implements Serializable,Department{
	private static final long serialVersionUID = -884153490003744964L;
	private String id;
	private String longName;
	private String name;
	private String parentLongName;
	private boolean enabled=false;
	private Set<Department> children=new HashSet<>();
	public KingdeeDepartment(String id,String longName) {
		this.id=id;
		this.longName=longName;
		int lastIndex = longName.lastIndexOf("/");
		if(lastIndex!=-1){
			this.name = longName.substring(lastIndex+1,longName.length());
			this.parentLongName = longName.substring(0,lastIndex);;
		}else{
			this.name = longName;
			this.parentLongName = null;
		}
	}
	
	public String getId() {
		return id;
	}

	public String getName() {
		return name;
	}
	
	public String getLongName() {
		return longName;
	}
	
	public String getParentLongName() {
		return this.parentLongName;
	}

	public Set<Department> getChildren() {
		return Collections.unmodifiableSet(children);
	}
    
	public boolean isEnabled() {
		boolean result=this.enabled;
		for(Department child:this.getChildren()){
			result=(result||child.isEnabled());
		}
		return result;
	}

	public void setEnabled(boolean enabled) {
		this.enabled=enabled;
	}
	@Override
	public void removeChildren(Collection<Department> children) {
		this.children.removeAll(children);
	}
	@Override
	public void addChild(Department department) {
		this.children.add(department);
	}
}
